import 'package:flutter/material.dart';
import 'package:flutter_application_vscode/ColorDemo.dart';
import 'package:flutter_application_vscode/ThemeTestPage.dart';
import 'DialogExamplePage.dart';
import 'DoubleBackExitPage.dart';
import 'FutureBuilderPage.dart';
import 'GestureDetectorPage.dart';
import 'ListenerPage.dart';
import 'StreamBuilderPage.dart';
import 'ValueListenableBuilderPage.dart';
import 'list_view_demo.dart';
import 'scroll_page.dart';
import 'AnimatedListExample.dart';
import 'CustomScrollViewExample.dart';
import 'GridView.dart';
import 'NestedScrollViewExample.dart';
import 'PageViewExample.dart';
import 'ScrollNotificationTestRoute.dart';
import 'TabBarViewExample.dart';
import 'random_words_widget.dart';
import 'button_page.dart';
import 'image_page.dart';
import 'blend_repeat_page.dart';
import 'text.dart';
import 'switch_checkbox_page.dart';
import 'textfield.dart';
import 'progress.dart';
import 'padding_page.dart';
import 'decorated_box_page.dart';
import 'transform_page.dart';
import 'container_page.dart';
import 'clip_page.dart';
import 'scaffold_page.dart';

const Color kButtonTextColor = Colors.orange;
const EdgeInsets _buttonPadding = EdgeInsets.all(16);
const TextStyle _buttonTextStyle = TextStyle(
  inherit: false,
  color: kButtonTextColor,
);

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  // 将 GlobalKey 声明为类的成员变量
  final GlobalKey<RandomWordsWidgetState> _randomWordsKey = GlobalKey();

  void _incrementCounter() {
    setState(() => _counter++);
    _randomWordsKey.currentState?.refresh();
  }

  void _navigateTo<T extends Widget>(Widget Function() builder) {
    Navigator.push(context, MaterialPageRoute(builder: (context) => builder()));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text('You have pushed the button this many times:'),
              Text(
                '$_counter',
                style: Theme.of(context).textTheme.headlineMedium,
              ),
              RandomWordsWidget(key: _randomWordsKey),
              Card(
                child: Padding(
                  padding: _buttonPadding,
                  child: ListView(
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    children: [
                      _buildNavButton("1.打开文本组件页面", () => const TextPage()),
                      _buildNavButton("2.打开按钮页面", () => const ButtonPage()),
                      _buildNavButton("3.打开图片界面：展示不同fit效果", () => const ImagePage()),
                      _buildNavButton("4.打开图片界面：展示不同混合模式 & 重复效果", () => const BlendRepeatPage()),
                      _buildNavButton("5.打开选择单选开关和复选框组件页面", () => const SwitchCheckboxPage()),
                      _buildNavButton("6.打开输入框组件页面", () => const TextFieldPage()),
                      _buildNavButton("7.打开进度条演示页面", () => const ProgressPage()),
                      _buildNavButton("8.打开Padding演示页面", () => const PaddingPage()),
                      _buildNavButton("9.打开DecoratedBox演示页面", () => const DecoratedBoxPage()),
                      _buildNavButton("10.打开Transform演示页面", () => const TransformPage()),
                      _buildNavButton("11.打开Container演示页面", () => const ContainerPage()),
                      _buildNavButton("12.打开Clip演示页面", () => const ClipPage()),
                      _buildNavButton("13.打开Scaffold布局组件页面", () => const ScaffoldPage()),
                      _buildNavButton("14.打开SingleChildScrollViewPage页面", () => const SingleChildScrollViewPage()),
                      _buildNavButton("15.打开高级列表演示页面", () => const ListViewDemo()),
                      _buildNavButton("16.打开ListView滚动监听演示页面", () => const ScrollNotificationTestRoute()),
                      _buildNavButton("17.打开AnimatedList演示页面", () => const AnimatedListPage()),
                      _buildNavButton("18.打开GridView演示页面", () => const GridViewPage()),
                      _buildNavButton("19.打开PageView与页面缓存演示页面", () => const PageViewExample()),
                      _buildNavButton("20.打开TabBarView演示页面", () => const TabBarViewExample()),
                      _buildNavButton("21.打开CustomScrollView & Slivers演示页面", () => const CustomScrollViewExample()),
                      _buildNavButton("22.打开NestedScrollView演示页面", () => const NestedScrollViewExample()),
                      _buildNavButton("23.打开导航返回拦截演示页面", () => DoubleBackExitPage()),
                      _buildNavButton("24.打开Color演示页面", () => ColorDemo()),
                      _buildNavButton("25.打开Theme演示页面", () => ThemeTestPage()),
                      _buildNavButton("26.打开ValueListenableBuilder演示页面", () => ValueListenableBuilderPage()),
                      _buildNavButton("27.打开异步UI更新：FutureBuilder演示页面", () => FutureBuilderPage()),
                      _buildNavButton("28.打开异步UI更新：StreamBuilder演示页面", () => StreamBuilderPage()),
                      _buildNavButton("29.打开Dialog示例页面", () => DialogExamplePage()),
                      _buildNavButton("30.打开Listener监听触摸事件演示页面", () => ListenerPage()),
                      _buildNavButton("31.打开GestureDetector手势检测演示页面", () => GestureDetectorPage()),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }

  Widget _buildNavButton(String text, Widget Function() pageBuilder) {
    return TextButton(
      onPressed: () => _navigateTo(pageBuilder),
      child: Text(text, style: _buttonTextStyle),
    );
  }
}
